﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gmis.Public;
namespace Gmis.Web.Manage.Village
{
    /// <summary>
    /// News 的摘要说明
    /// </summary>
    public class News : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["Action"].ToLower();

            if (string.IsNullOrEmpty(action))
            {
                context.Response.Write(CommonFunction.failure("无指定操作"));
            }
            else
            {
                //ProcessAction();
                switch (action.ToLower())
                {
                    case "add":
                        ProcessAddAction(context);
                        break;

                    case "delete":
                        ProcessDeleteAction(context);
                        break;
                    case "deleteall":
                        ProcessDeleteAllAction(context);
                        break;
                    case "edit":
                        ProcessEditAction(context);
                        break;
                }
            }
        }

        private void ProcessDeleteAllAction(HttpContext context)
        {
            //throw new NotImplementedException();
            string ids=context.Request.Form["ids"];
            if (ids != string.Empty)
            {

                string[] idsList = ids.Split(',');
                ids = string.Empty;
                for(int i = 0; i < idsList.Length;i++)
                {
                    
                    idsList[i] = "'" + idsList[i] + "'";
                    if (i != 0)
                    {
                        ids += ",";
                    }
                    ids += idsList[i];
                     
                }
               
                Gmis.BLL.News bllNews = new BLL.News();
                try
                {
                    if (bllNews.DeleteList(ids))
                    {
                        context.Response.Write(CommonFunction.success());
                    }
                    else
                    {
                        context.Response.Write(CommonFunction.failure());
 
                    }
                }
                catch
                {
                    context.Response.Write(CommonFunction.failure());
 
                }
            }

        }

        private void ProcessEditAction(HttpContext context)
        {
            //throw new NotImplementedException();
            Gmis.Model.News model = GetModelInfo(context);
            model.sId = context.Request["NewsId"];
            Gmis.BLL.News bllNews = new BLL.News();
            try
            {
                bllNews.Update(model);
                context.Response.Write(CommonFunction.success());

            }
            catch
            {
                context.Response.Write(CommonFunction.failure());

            }
        }

        private void ProcessDeleteAction(HttpContext context)
        {
            //throw new NotImplementedException();
            string newsId = context.Request["NewsId"];
            Gmis.BLL.News bllNews = new BLL.News();
            try
            {
                if (bllNews.Delete(newsId))
                {
                    context.Response.Write(CommonFunction.success());
                }
                else
                {
                    context.Response.Write(CommonFunction.failure());
 
                }

            }
            catch
            {
                context.Response.Write(CommonFunction.failure());

            }
        }

        private void ProcessAddAction(HttpContext context)
        {
            //throw new NotImplementedException();
            //throw new NotImplementedException();
            Gmis.Model.News model = GetModelInfo(context);

            Gmis.BLL.News bllNews = new BLL.News();
            try
            {
                bllNews.Add(model);
                context.Response.Write(CommonFunction.successAndCloseCurrent("", "jbsxBox"));
               
            }
            catch
            {
                context.Response.Write(CommonFunction.failure());

            }

        }
        private Gmis.Model.News GetModelInfo(HttpContext context)
        {
            string sId = CommonFunction.GetGuid();
            string sColumn = context.Request["ColumnId"];
            string sType = "";
            string sTitle = context.Request["sTitle"];
            string sContent = context.Request["sContent"];
            string sAuthor = "";
            DateTime dDate = DateTime.Now;
            string sCreator = "";
            bool lEnable = context.Request["lEnable"]==null ? false:true;
            string sVillage = "";
            string sComment = "";
            DateTime dCreate = DateTime.Now ;

            Gmis.Model.News model = new Gmis.Model.News();
            model.sId = sId;
            model.sColumn = sColumn;
            model.sType = sType;
            model.sTitle = sTitle;
            model.sContent = sContent;
            model.sAuthor = sAuthor;
            model.dDate = dDate;
            model.sCreator = sCreator;
            model.lEnable = lEnable;
            model.sVillage = sVillage;
            model.sComment = sComment;
            model.dCreate = dCreate;
            return model;

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}